# -*-coding: Utf-8 -*-
# @File : file
# author: liaohf
# Time：2024/9/22
from core.Response import success, fail
from database.models.base import FileData
from schemas import file
from fastapi import APIRouter

router = APIRouter()


@router.post("/uploadFile", summary="文件上传")
async def file(post: file.File):
    """
    文件上传
    :param post: UploadFile
    :return:
    """
    # 文件上传
    sql_file_data = await FileData.create(**post.dict())
    if not sql_file_data:
        return fail(msg=f"上传文件失败!")
    return success(msg=f"上传文件成功!", data={'id': sql_file_data.id})


@router.get("/getFileById", summary="获取文件")
async def get_file(file_id):
    """
    文件上传
    :param file_id
    :return:
    """
    # 文件上传
    sql_file_data = await FileData.filter(id=file_id).all()
    if not sql_file_data:
        return fail(msg=f"上传文件失败!")
    return success(msg=f"上传文件成功!", data=sql_file_data[0])
